<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\WhitemService;
use Illuminate\Http\Request;

class WhitemController extends BaseController
{
    //工时子级

    //选项
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['config_whid'] = $request->post('config_whid', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $res = WhitemService::getOne($id, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //计算
    public function getDelay(Request $request)
    {
        $start = $request->post('start', '');
        $end = $request->post('end', '');
        $res = WhitemService::getDelay($start, $end);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['uid'] = $request->post('uid', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['wh_id'] = $request->post('wh_id', 0);
        $pas['config_whid'] = $request->post('config_whid', 0);
//        $pas['title'] = $request->post('title', '');
        $pas['cnt_id'] = $request->post('cnt_id', 0);
        $pas['starthour'] = $request->post('starthour', '');
        $pas['endhour'] = $request->post('endhour', '');
        //$pas['delay'] = $request->post('delay', 0);
        if ($pas['starthour'] > $pas['endhour']) {
            return self::toJson(-2, '开始时间不能大于结束时间');
        }
        if (!$pas['cnt_id']) {
            return self::toJson(-2, '内容必须的');
        }
        $resDelay = WhitemService::chongfu($id, $pas['wh_id'], $pas['uid'], $pas['starthour'], $pas['endhour']);
        //ddd($resDelay);
        if ($resDelay['code'] < 0) {
            return self::toJson(-2, $resDelay['msg']);
        }
        if ($id) {
            $res = WhitemService::modify($id, $pas);
        } else {
            $res = WhitemService::add($pas);
        }
        //ddd($resDelay, $res);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhitemService::del($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //获取内容
    public function getCntList(Request $request)
    {
        $id = $request->post('wh_id', 0);
        $res = WhitemService::getCntList($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //内容搜索
    public function ssCnt(Request $request)
    {
        $wh_id = $request->post('wh_id', 0);
        $key = $request->post('key', '');
        $res = WhitemService::ssCnt($wh_id, $key);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }
}